<html>
<head><meta charset="utf-8"><title>PartialOrd for *const T · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/PartialOrd.20for.20*const.20T.html">PartialOrd for *const T</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="146838687"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/PartialOrd%20for%20%2Aconst%20T/near/146838687" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/PartialOrd.20for.20*const.20T.html#146838687">(Nov 06 2018 at 01:48)</a>:</h4>
<p>Does anybody know how the implementation of <code>PartialOrd</code> works for <code>*const T</code> pointers? It looks recursive to me. For example, less than is defined <a href="https://github.com/rust-lang/rust/blob/65e485d8f1d28102b426c9d6d82f835cd6470d3e/src/libcore/ptr.rs#L2620" target="_blank" title="https://github.com/rust-lang/rust/blob/65e485d8f1d28102b426c9d6d82f835cd6470d3e/src/libcore/ptr.rs#L2620">here</a> as</p>
<div class="codehilite"><pre><span></span><span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span>: <span class="o">?</span><span class="nb">Sized</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">PartialOrd</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">lt</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">other</span>: <span class="kp">&amp;</span><span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="n">T</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">bool</span> <span class="p">{</span><span class="w"> </span><span class="o">*</span><span class="bp">self</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="o">*</span><span class="n">other</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<p>So doesn't the body of <code>lt()</code> call itself since <code>*&amp;*const T</code> is just a <code>*const T</code>? Or is there deref coercion magic going on?</p>



<a name="146850317"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/PartialOrd%20for%20%2Aconst%20T/near/146850317" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/PartialOrd.20for.20*const.20T.html#146850317">(Nov 06 2018 at 07:37)</a>:</h4>
<p>This is true for all primitives. These operations are not actually lowered to a call to the trait method. You can see this "exposed" if you add a println to the method and do the operation. The println won't show up for the primitive</p>



<a name="146866307"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/PartialOrd%20for%20%2Aconst%20T/near/146866307" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/PartialOrd.20for.20*const.20T.html#146866307">(Nov 06 2018 at 14:02)</a>:</h4>
<p>Thanks <span class="user-mention" data-user-id="124288">@Oli</span>! I don't see any special attributes or anything on these methods. How does the compiler know not to lower these to method calls?</p>



<a name="146867120"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/PartialOrd%20for%20%2Aconst%20T/near/146867120" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/PartialOrd.20for.20*const.20T.html#146867120">(Nov 06 2018 at 14:19)</a>:</h4>
<p>The transformation in <a href="https://github.com/rust-lang/rust/blob/master/src/librustc_mir/hair/cx/expr.rs#L367" target="_blank" title="https://github.com/rust-lang/rust/blob/master/src/librustc_mir/hair/cx/expr.rs#L367">HAIR</a> checks whether some earlier passes decided this. Not sure where exactly that decision is happening, but I'm fairly certain it's simply hardcoded</p>



<a name="147307814"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/PartialOrd%20for%20%2Aconst%20T/near/147307814" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/PartialOrd.20for.20*const.20T.html#147307814">(Nov 08 2018 at 15:45)</a>:</h4>
<p>The code is here:</p>
<p><a href="https://github.com/rust-lang/rust/blob/653da4fd006c97625247acd7e076d0782cdc149b/src/librustc_typeck/check/writeback.rs#L122-L172" target="_blank" title="https://github.com/rust-lang/rust/blob/653da4fd006c97625247acd7e076d0782cdc149b/src/librustc_typeck/check/writeback.rs#L122-L172">https://github.com/rust-lang/rust/blob/653da4fd006c97625247acd7e076d0782cdc149b/src/librustc_typeck/check/writeback.rs#L122-L172</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>